//
//  TextFieldUnit.m
//  NetworkTaxiDriver
//
//  Created by thinkjoy on 16/9/21.
//  Copyright © 2016年 Thinkjoy_RS. All rights reserved.
//

#import "TextFieldUnit.h"

@implementation TextFieldUnit

#pragma mark    判断textField是否允许继续输入,禁止输入空格
+ (BOOL)isEditorNotSpaceChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLen
{
    
    //如果输入空格,不允许输入
    if([string isEqualToString:@" "]){
        return NO;
    }
    //如果包括空格的粘贴也禁止输入
    else if ([string rangeOfString:@" "].location!=NSNotFound) {
//        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        return NO;
    }
    //如果长度大于最大长度,禁止输入
    else if (range.location>maxLen-1) {
        return NO;
    }
    //如果粘贴后的字符串长度大于最大长度,禁止输入
    else if (range.location+string.length>maxLen){
        return NO;
    }
    return YES;
}

#pragma mark    判断textField是否允许继续输入,允许输入空格
+ (BOOL)isEditorEnableSpaceChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLen
{
    //如果长度大于最大长度,禁止输入
    if (range.location>maxLen-1) {
        return NO;
    }
    //如果粘贴后的字符串长度大于最大长度,禁止输入
    else if (range.location+string.length>maxLen){
        return NO;
    }
    return YES;
}

#pragma mark    --修改输入文本框的Placeholder的大小和颜色
- (UITextField *)rewriteTextFieldPlaceholder:(UITextField *)textField WithColor:(UIColor *)color  withFontSize:(CGFloat)fontsize
{
    if (color==nil&&fontsize<=0) {
        return textField;
    }else if (color==nil){
        //只修改大小
        NSString *holderText = textField.placeholder;
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:holderText];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:fontsize]
                            range:NSMakeRange(0, holderText.length)];
        textField.attributedPlaceholder = placeholder;
    }else if (fontsize<=0){
        //只修改颜色
        NSString *holderText = textField.placeholder;
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:color
                            range:NSMakeRange(0, holderText.length)];
        textField.attributedPlaceholder = placeholder;
    }else{
        //全部修改
        NSString *holderText = textField.placeholder;
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:color
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:fontsize]
                            range:NSMakeRange(0, holderText.length)];
        textField.attributedPlaceholder = placeholder;
    }
    
    //方法1
    //    [textField setValue:color  forKeyPath:@"_placeholderLabel.textColor"];
    //    [textField setValue:[UIFont systemFontOfSize:fontsize] forKeyPath:@"_placeholderLabel.font"];
    
    
    return textField;
}


@end
